package com.instwall.callbase.tool;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author csw
 */
public class DateUtill {
    /**
     * yyyyMMddHHmmss
     */
    public static final String LONG = "yyyy:MM:dd:HH:mm:ss";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String LONG_ = "yyyy-MM-dd HH:mm:ss";

    /**
     * yyyyMMddHHmmssSSS
     */
    public static final String TIMESTAMP = "yyyyMMddHHmmssSSS";

    /**
     * yyyy-MM-dd HH:mm:ss.SSS
     */
    public static final String TIMESTAMP_ = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * yyyyMMdd
     */
    public static final String MEDIUM = "yyyyMMdd";// medium

    /**
     * yyyy-MM-dd
     */
    public static final String MEDIUM_ = "yyyy-MM-dd";

    public static int oneDay24ms = 86400000;

    /**
     * @param dateString yyyyMMddHHmmssSSS
     * @return Date
     */
    public static Date parse(String dateString) {
        Date date = null;
        if (dateString != null) {
            SimpleDateFormat format = new SimpleDateFormat(TIMESTAMP);

            try {
                date = format.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    public static String getGMtTime(Calendar now) {
        //        Calendar now=Calendar.get();
        /**
         * 获取当前时区时间距历元元年的毫秒数
         */
        long srcTime = now.getTimeInMillis();
        /**
         * TimeZone.getDefault().getRawOffset()为当前默认时区（即GMT+8）与GMT
         * 时区的原始偏差（的毫秒数）
         * 而下式的运算即为当前时区时间所对应的格林威治时间（转换为GMT对应的时间）
         */
        long targetTime = srcTime - TimeZone.getDefault().getRawOffset();
        /**
         * 创建Date
         */
        Date newDate = new Date(targetTime);
        /**
         * format the date
         */
        DateFormat df = new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss");
        String str = df.format(newDate);
        System.out.println("newDate:" + str);
        return str;
    }


    public static int getWeek() {
        GregorianCalendar gregorianCalendar = new GregorianCalendar(Locale.CHINA);
        int week = gregorianCalendar.get(Calendar.DAY_OF_WEEK) - 1;
        return week;
    }

    public static boolean compareTime(String s1, String s2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(s1));
            c2.setTime(df.parse(s2));
        } catch (ParseException e) {
            System.err.println("格式不正确");
        }
        int result = c1.compareTo(c2);
        //		result==0  "c1相等c2"     result<0  "c1小于c2"    result >0 "c1大于c2";
        if (result <= 0) {
            return true;
        } else {
            return false;
        }
    }


    public static String getDate() {
        SimpleDateFormat dateFormat24 = new SimpleDateFormat("yyyy-MM-dd ", Locale.CHINA);
        return dateFormat24.format(Calendar.getInstance().getTime());
    }

    public static String getDetailDate() {
        SimpleDateFormat sf = new SimpleDateFormat(LONG_, Locale.CHINA);
        return sf.format(Calendar.getInstance().getTime());
    }

}